<?php 
/**
 * creates the layout for category menue item
 * @return string layout
 */
function CreateCategoryLayout($auth) {
	try {
		$mysql = $auth->GetSqlConnection();
		$description = GetLanguageSpecificText($mysql, "description_attribute", LANGUAGE);
		
		$output = '';
		
		$output .= CreateToolbar($mysql);
		
		$output .= '<form id="form_create_category" action="' . COSTPOINTS . '" name="form_create_category" method="post" style="display:none">';
		$output .= '<table >';
		$output .= '<tr>';
		$output .= '<td><span style="color: #61021B; font-weight:bold">Name:</span></td>';
		$output .= '<td><input type="text" id="name" class="text ui-widget-content ui-corner-all" style="width:200px; height:15px;" /></td>';
		$output .= '</tr>';
		$output .= '<tr>';
		$output .= '<td>' . $description . ':</td>';
		$output .= '<td><textarea id="description" rows="10" class="text ui-widget-content ui-corner-all" style="width:200px;" /></td>';
		$output .= '</tr>';
		$output .= '</table>';
		$output .= '</form>';
		
		$output .= CreateCategoryTable($mysql, $description);
		
		return $output;
	} catch (Exception $e) {
		return $e->getMessage();
	}
}



/**
 * creates toolbar for form
 * @param		mixed	 $mysql
 * @return		string	 $output
 */
function CreateToolbar($mysql) {
	$output = '';

	$new = GetLanguageSpecificText($mysql, "new_attribute", LANGUAGE);
	$save = GetLanguageSpecificText($mysql, "save_attribute", LANGUAGE);

	$output .= '<span id="itemtoolbar" class="ui-widget-header ui-corner-all">';
	$output .= '<button id="btnNew" onclick="HandleNewCategory();">' . $new . '</button>';
	$output .= '<button id="btnSave" onclick="HandleSaveCategory();">' . $save . '</button>';
	$output .= '</span><br /><br />';

	return $output;
}


/**
 * creates output table for categories
 * @param 		mixed		 $mysql			sql connection
 * @param 		string		 $description	description header for table accordingly the language
 */
function CreateCategoryTable($mysql, $description) {
	$output = '';
	
	$output .= '<div style="margin-top:40px" id="category_table">';
	$output .= '<table class="outputTable" style="width:600px">';
	$output .= '<tr>';
	$output .= '<th>&nbsp;</th>';
	
	$output .= '<th>Name</td>';
	$output .= '<th>' . $description . '</th>';
	$output .= '</tr>';
	
	$sql = 'select * from ' . DB . '.Category where person_id=? order by name';
	$params = array(
			GetPersonId($mysql, $_COOKIE['EMAIL']));
	
	try {
		$mysql->Execute($sql, $params);
		
		while (($category = $mysql->Fetch())) {
			$output .= '<tr><td style="width:35px">' . GenerateUIIcon('ui-icon-pencil', NULL, 'HandleEditCategory(' . $category->category_id . ');') . GenerateUIIcon('ui-icon-trash', NULL, 'DeleteCategory(' . $category->category_id . ');') . '</td><td>' . $category->name . '</td>';
			$output .= '<td>' . $category->description . '</td></tr>';
		}
	
	} catch (Exception $e) {
		return $e->getMessage();
	}
	
	$output .= '</table>';
	$output .= '</div>';
	
	return $output;
}

/**
 * saves the category
 * @return		array		$result
 */
function SaveCategory($categoryId = NULL, $auth) {
	$type = (isset($_REQUEST['TYPE'])) ? $_REQUEST['TYPE'] : NULL;
	$description = (isset($_REQUEST['DESCRIPTION'])) ? $_REQUEST['DESCRIPTION'] : NULL;
	$name = (isset($_REQUEST['NAME'])) ? $_REQUEST['NAME'] : NULL;
	
	switch ($type) {
		case 'NEW' :
			$personId = $auth->GetUserId();
			try {
				$mysql = $auth->GetSqlConnection();
				
				$categoryId = GetNextID($mysql, 'Category', 'category_id');
				$sql = 'insert into ' . DB . '.Category(category_id, name, person_id, description) values (?, ?, ?, ?)';
				$params = array(
						$categoryId, 
						$name, 
						$personId, 
						$description);
				
				$mysql->Execute($sql, $params);
				$result = array(
						'RESPONSE' => 'SUCCESS', 'CATEGORY_ID' => $categoryId);
				
				
			} catch (Exception $e) {
				$result = array(
						'RESPONSE' => 'ERROR', 
						'MESSAGE' => $e->getMessage());
			}
			break;
		
		case 'EDIT' :
			if (isset($categoryId)) {
				try {
					$mysql = $auth->GetSqlConnection();
					
					$sql = 'update ' . DB . '.Category set name=?, description=? where category_id=?';
					$params = array(
							$name, 
							$description, 
							$categoryId);
					
					$mysql->Execute($sql, $params);
					
					$result = array(
							'RESPONSE' => 'SUCCESS', 'CATEGORY_ID' => $categoryId);
				
				} catch (Exception $e) {
					$result = array(
							'RESPONSE' => 'ERROR', 
							'MESSAGE' => $e->getMessage());
				}
			} else {
				$result = array(
						'RESPONSE' => 'ERROR', 
						'MESSAGE' => 'CategoryId is not set!');
			}
	}
	
	echo json_encode($result);
}


/**
 * reloads the category table after deleating or saving
 */
function ReloadCategoryTable($auth) {
	$output = '';
	try {
		$mysql = $auth->GetSqlConnection();
		$description = GetLanguageSpecificText($mysql, "description_attribute", LANGUAGE);
		$output = CreateCategoryTable($mysql, $description);
	} catch (Exception $e) {
		$output = $e->getMessage();
	}
	
	return $output;
}


/**
 * deletes category
 * @param 		int		$categoryId
 * @return		array	$result
 */
function DeleteCategory($categoryId, $auth) {
	if (isset($categoryId)) {
		try {
			$mysql = $auth->GetSqlConnection();
			
			$sql = 'delete from ' . DB . '.Category where category_id=?';
			$params = array(
					$categoryId);
			
			$mysql->Execute($sql, $params);
			
			$result = array(
					'RESPONSE' => 'SUCCESS');
		
		} catch (Exception $e) {
			$result = array(
					'RESPONSE' => 'ERROR', 
					'MESSAGE' => $e->getMessage());
		}
	} else {
		$result = array(
				'RESPONSE' => 'ERROR', 
				'MESSAGE' => 'CategoryId is not set!');
	}
	
	echo json_encode($result);
}


function GetCategoryData($categoryId, $auth) {
	if (isset($categoryId)) {
		try {
			$mysql = $auth->GetSqlConnection();
			
			$sql = 'select name, description from ' . DB . '.Category where category_id=?';
			$params = array(
					$categoryId);
			
			$mysql->Execute($sql, $params);
			$category = $mysql->Fetch();
			$result = array(
					'RESPONSE' => 'SUCCESS', 
					'NAME' => $category->name, 
					'DESCRIPTION' => $category->description);
		
		} catch (Exception $e) {
			$result = array(
					'RESPONSE' => 'ERROR', 
					'MESSAGE' => $e->getMessage());
		}
	} else {
		$result = array(
				'RESPONSE' => 'ERROR', 
				'MESSAGE' => 'CategoryId is not set!');
	}
	echo json_encode($result);
}
?>